

using com.tc.util;
using com.tc.zigbee.zcl.command;
using com.tc.gateway.device;
using System.Collections.Generic;
namespace com.tc.zigbee.entity
{
    public class ZigbeeScene : Scene
    {
    
      
        public short getTransitionTime()
        {
            return transitionTime;
        }
        public void setTransitionTime(short transitionTime)
        {
            this.transitionTime = transitionTime;
        }
        public byte[] getSceneName()
        {
            return sceneName;
        }
        public void setSceneName(byte[] sceneName)
        {
            this.sceneName = sceneName;
        }
        public List<SceneClusterRecord> getClusterRecords()
        {
            return clusterRecords;
        }
        public void setClusterRecords(List<SceneClusterRecord> clusterRecords)
        {
            this.clusterRecords = clusterRecords;
        }
        short transitionTime;
        byte[] sceneName;
        List<SceneClusterRecord> clusterRecords = new List<SceneClusterRecord>();
        byte length()
        {
            byte s = 0;
            foreach (SceneClusterRecord v in clusterRecords)
            {
                s += v.size();
            }
            return (byte)(s + 5 + sceneName.Length + 1);
        }

        public byte[] payload(){
			byte[] p = new byte[length()];
			byte[] buf = DataUtil.bytesValue(group);
			int i = 0;
			p[i++] = buf[0];
			p[i++] = buf[1];
			p[i++] = scene;
			buf = DataUtil.bytesValue(this.transitionTime);
			p[i++] = buf[0];
			p[i++] = buf[1];
			p[i++] = (byte)sceneName.Length;
			foreach(byte t in sceneName)
				p[i++] = t;
			foreach(SceneClusterRecord asc in clusterRecords){
				buf = DataUtil.bytesValue(asc.getClusterId());
				p[i++] = buf[0];
				p[i++] = buf[1];
				p[i++] = buf[0];
				p[i++] = asc.valueLength();
				foreach(DataValue t in asc.attributeValues){
					foreach(byte b in t.value){
						p[i++] = b;
					}
				}
				
			}
		return p;	
		}
        public ZigbeeScene(byte[] data)
        {
            int i = 0;
            byte status = data[i++];

            byte[] buf = new byte[2];
            buf[0] = data[i++];
            buf[0] = data[i++];
            group = DataUtil.shortValue(buf);
            scene = data[i++];
            if (status != 0)
                return;
            buf[0] = data[i++];
            buf[0] = data[i++];
            this.transitionTime = DataUtil.shortValue(buf);
            byte len = data[i++];
            sceneName = new byte[len];
            for (int j = 0; j < len; j++)
                sceneName[j] = data[i++];
            while (i < data.Length)
            {
                SceneClusterRecord r = new SceneClusterRecord();
                buf[0] = data[i++];
                buf[0] = data[i++];
                short cid = DataUtil.shortValue(buf);
                r.setClusterId(cid);

                ClusterMeta meta = SceneExtensionMeta.getClusterMeta(cid);
                byte el = data[i++];
                int ss = 0;
                int ai = 0;
                while (ss < el)
                {

                    DataType dt = meta.AttributeDataTypes[ai++];
                    byte[] vb = new byte[dt.getLength()];
                    for (int x = 0; x < dt.getLength(); x++)
                    {
                        vb[x] = data[i++];
                    }
                    DataValue v = new DataValue(dt.getLength(), vb);
                    r.getAttributeValues().Add(v);
                    ai++;
                    ss += dt.getLength();

                }
                clusterRecords.Add(r);

            }

        }
        public ZigbeeScene()
        {
            // TODO Auto-generated constructor stub
        }
    }
   public class SceneClusterRecord
    {
        short clusterId;
        public List<DataValue> attributeValues = new List<DataValue>();
        public short getClusterId()
        {
            return clusterId;
        }
        public void setClusterId(short clusterId)
        {
            this.clusterId = clusterId;
        }

        public List<DataValue> getAttributeValues()
        {
            return attributeValues;
        }
        public void setAttributeValues(List<DataValue> attributeValues)
        {
            this.attributeValues = attributeValues;
        }
        public byte size()
        {

            return (byte)(valueLength() + 3);
        }
        public byte valueLength()
        {
            byte s = 0;
            foreach (DataValue v in attributeValues)
            {
                s += (byte)v.value.Length;
            }
            return s;
        }

    }
}